package com.sheep.jucdemo.part11.part11_3;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Date 2022/5/24 23:15
 * @Created by sheep
 */
public class Test11_3_1 {
    // 创建map，key为topic，value为设备列表
    static ConcurrentHashMap<String, List<String>> map = new ConcurrentHashMap<>();

    public static void main(String[] args) {
        // 进入直播间topic1，线程threadOne
        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                CopyOnWriteArrayList<String> list1 = new CopyOnWriteArrayList<>();
                list1.add("device1");
                list1.add("device2");
                map.put("topic1", list1);
                System.out.println(map);
            }
        });

        // 进入直播间topic1，线程threadTwo
        Thread threadTwo = new Thread(new Runnable() {
            @Override
            public void run() {
                CopyOnWriteArrayList<String> list1 = new CopyOnWriteArrayList<>();
                list1.add("device11");
                list1.add("device22");
                map.put("topic1", list1);
                System.out.println(map);
            }
        });

        // 进入直播间topic2，线程threadThree
        Thread threadThree = new Thread(new Runnable() {
            @Override
            public void run() {
                CopyOnWriteArrayList<String> list1 = new CopyOnWriteArrayList<>();
                list1.add("device111");
                list1.add("device222");
                map.put("topic2", list1);
                System.out.println(map);
            }
        });

        // 启动线程
        threadOne.start();
        threadTwo.start();
        threadThree.start();
    }
}
